﻿Public Class t01_tugas

    Dim acr As New ModLibrary.All_Function_Cls
    Public BS As BindingSource

    Private Sub t01_tugas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadNow()
        acr.DGVColDropDownDate(TugasDataGridView, 2, "TGL_AWAL", "TGL_AWAL", "TGL_AWAL", DataGridViewContentAlignment.MiddleCenter)
        acr.DGVColDropDownDate(TugasDataGridView, 3, "TGL_AKHIR", "TGL_AKHIR", "TGL_AKHIR", DataGridViewContentAlignment.MiddleCenter)
    End Sub

    Sub LoadNow()
        BS = TugasBindingSource
        'TODO: This line of code loads data into the 'DS_t01_tugas.master_sopir' table. You can move, or remove it, as needed.
        Me.Master_sopirTableAdapter.Fill(Me.DS_t01_tugas.master_sopir)
        'TODO: This line of code loads data into the 'DS_t01_tugas.tugas' table. You can move, or remove it, as needed.
        Me.TugasTableAdapter.Fill(Me.DS_t01_tugas.tugas)
        BS.MoveLast()
    End Sub

    Function Form_Valid()
        Dim Pesan, Validasi As String
        Pesan = "" : Validasi = ""
        If ID_TUGASTextBox.Text = "" Then Pesan += "- ID_Tugas Cannot Empty " & vbNewLine
        If ID_SOPIRComboBox.Text = "" Then Pesan += "- ID_Sopir Month Cannot Empty " & vbNewLine
        If TGL_AWALTextBox.Text = "" Then Pesan += "- TGL_Awal Month Cannot Empty " & vbNewLine
        If TGL_AKHIRTextBox.Text = "" Then Pesan += "- TGL_Akhir Month Cannot Empty " & vbNewLine
        If APPROVALTextBox.Text = "" Then Pesan += "- Approval Cannot Empty " & vbNewLine
        If Pesan <> "" Then
            Validasi = "Input Data Is Not Correct / Not Complete on : " & vbNewLine & vbNewLine & Pesan
        End If
        Return Validasi
    End Function

    Private Sub ID_SOPIRComboBox_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ID_SOPIRComboBox.DrawItem
        On Error Resume Next
        ' Draw the default background
        e.DrawBackground()
        ' The ComboBox is bound to a DataTable,
        ' so the items are DataRowView objects.
        Dim drv As DataRowView = CType(ID_SOPIRComboBox.Items(e.Index), DataRowView)
        ' Retrieve the value of each column.
        Dim id1 As String = drv("ID_SOPIR").ToString()
        Dim id2 As String = drv("NAMA").ToString()

        ' +++++++++++++++++++++++++++++++++++++++++
        ' Get the bounds for the first column
        Dim r1 As Rectangle = e.Bounds
        r1.Width = r1.Width * 0.2
        ' Draw the text on the first column
        Using sb As SolidBrush = New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(id1, e.Font, sb, r1)
        End Using
        ' Draw a line to isolate the columns
        Using p1 As Pen = New Pen(Color.Gainsboro)
            e.Graphics.DrawLine(p1, r1.Right - 2, 0, r1.Right - 2, r1.Bottom - 2)
        End Using

        ' +++++++++++++++++++++++++++++++++++++++++
        ' Get the bounds for the second column
        Dim r2 As Rectangle = e.Bounds
        r2.X = e.Bounds.Width * 0.2
        r2.Width = r2.Width * 0.8
        ' Draw the text on the second column
        Using sb As SolidBrush = New SolidBrush(e.ForeColor)
            e.Graphics.DrawString(id2, e.Font, sb, r2)
        End Using
        ' Draw a line to isolate the columns
        
    End Sub

    Private Sub Pnl_Date_2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pnl_Date_2.Click, Pnl_Date.Click
        Select Case CType(sender, Panel).Name.ToString
            Case "Pnl_Date"
                acr.DateBtn_Open(TGL_AWALTextBox)
            Case "Pnl_Date_2"
                acr.DateBtn_Open(TGL_AKHIRTextBox)
        End Select

    End Sub

    Private Sub TS_Refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        LoadNow()
    End Sub

    Sub Save()
        Try
            If Form_Valid() = "" Then
                Me.Validate()
                Me.TugasBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.DS_t01_tugas)
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Save_Special()
        Try
            Me.Validate()
            Me.TugasBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DS_t01_tugas)
            MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
            LoadNow()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Delete()
        If MsgBox("Delete this data ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Delete Confirmation") = MsgBoxResult.Yes Then
            BS.RemoveCurrent()
            If MsgBox("if you are sure... click [Yes] button to delete and save this changes..", MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "Pending Delete Data") = MsgBoxResult.Ok Then
                If Form_Valid() = "" Then
                    Save()
                Else
                    Save_Special()
                End If
            Else
                acr.MoveNavi("Load", 0, Me, BS)
            End If
        End If
    End Sub

    Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click, Btn_Refresh.Click, Btn_Previous.Click, Btn_Next.Click, Btn_Last.Click, Btn_First.Click, Btn_Delete.Click, Btn_Add.Click
        Select Case CType(sender, Button).Name.ToString
            Case "Btn_Save"
                Save()
            Case "Btn_Refresh"
                acr.MoveNavi("Load", 0, Me, BS)
            Case "Btn_Previous"
                acr.MoveNavi("MovePrev", 0, Me, BS)
            Case "Btn_Next"
                acr.MoveNavi("MoveNext", 0, Me, BS)
            Case "Btn_Last"
                acr.MoveNavi("MoveLast", 0, Me, BS)
            Case "Btn_First"
                acr.MoveNavi("MoveFirst", 0, Me, BS)
            Case "Btn_Add"
                acr.MoveNavi("Add", 0, Me, BS)
            Case "Btn_Delete"
                Delete()
        End Select
    End Sub

    Private Sub ruteDataGridView_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles TugasDataGridView.DataError
        e.Cancel = True
    End Sub

    Private Sub TGL_AWALTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TGL_AWALTextBox.TextChanged

    End Sub
End Class